﻿using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;

public class NotchInfo
{
    public string name;
    public int screen_height;
    public int screen_width;
    public int notch_width;
    public int notch_height;
    public int round_angle_radius;
    public int home_indicator_top;
    public int home_indicator_height;
    public int home_indicator_width;
}

public static class NotchInfoManager
{
    public const string dir = "notch";
    static Dictionary<string, NotchInfo> notchInfoMap = null;
    static string customModel;
    public static System.Func<string, string> externalLoader;

    public static void Init(bool force = false)
    {
        if (force || notchInfoMap == null)
        {
            string path = dir + "/notch_info";
            string str = null;
            if (externalLoader != null)
            {
                str = externalLoader(path);
            }

            if(str == null)
            {
                TextAsset textAsset = Resources.Load<TextAsset>(path);
                if (textAsset != null)
                {
                    str = textAsset.text;
                }
            }

            if (str != null)
            {
                notchInfoMap = JsonConvert.DeserializeObject<Dictionary<string, NotchInfo>>(str);
            }
        }
    }

    public static void SetCustomModel(string model)
    {
        customModel = model;
    }

    public static string GetCustomModel()
    {
        return customModel;
    }

    public static Vector4 GetViewportMargins()
    {
        NotchInfo temp = GetNotchInfo();
        if (temp == null)
        {
            return Vector4.zero;
        }

        Vector4 t = Vector4.zero;
        if(Screen.width > Screen.height)
        {
            t.x = (float)temp.notch_height / temp.screen_height;
            t.z = (float)temp.notch_height / temp.screen_height;
        }
        else
        {
            t.w = (float)temp.notch_height / temp.screen_height;
        }
        return t;
    }

    public static Vector4 GetScreenMargins()
    {
        Vector4 t = GetViewportMargins();
        t.x *= Screen.width;
        t.z *= Screen.width;
        return t;
    }

    public static float GetHomeIndicatorScreenMargin()
    {
        NotchInfo temp = GetNotchInfo();
        if (temp == null)
        {
            return 0;
        }

        return (float)temp.home_indicator_top / temp.screen_width * Screen.height;
    }

    public static NotchInfo GetNotchInfo()
    {
        Init(false);

        string curModel = customModel;
        if (string.IsNullOrEmpty(curModel))
        {
            curModel = SystemInfo.deviceModel;
        }

        NotchInfo temp = null;
        if (notchInfoMap == null || !notchInfoMap.TryGetValue(curModel, out temp))
        {
            return null;
        }
        return temp;
    }

    public static Dictionary<string, NotchInfo> GetNotchInfoMap()
    {
        Init(false);
        return notchInfoMap;
    }
}